package com.shine.app.ui;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.BookingConfirmationBean;
import com.shine.app.bean.request.FindBookingBean;
import com.shine.app.bean.responses.BookingConfirmationResponse;
import com.shine.app.commons.StringUtils;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class SlideFindTeeTimeActivity extends BaseActivity implements
		OnClickListener {
	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;
	@InjectView(R.id.email)
	private EditText emailEditText;
	@InjectView(R.id.number)
	private EditText numberEditText;
	@InjectView(R.id.submit)
	private Button findBookingButton;
	@InjectView(R.id.homePage)
	private TextView homePageTextView;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private FindBookingAsyncTask findBookingAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide_findbooking);
		Injector.get(this).inject();

		homePageTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		progressBar.setVisibility(View.GONE);
		head_backImageView.setOnClickListener(this);
		findBookingButton.setOnClickListener(this);
		homePageTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.head_back:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		case R.id.submit:
			findBookingButton.startAnimation(buttonAnimations);
			String email = emailEditText.getText().toString().trim();
			String confirmation = numberEditText.getText().toString().trim();
			if (!validation(email, confirmation)) {
				return;
			}
			FindBookingBean findBookingBean = new FindBookingBean(email,
					confirmation);
			findBookingAsyncTask = new FindBookingAsyncTask();
			findBookingAsyncTask.execute(findBookingBean);
			break;

		case R.id.homePage:
			Intent data = new Intent(Intent.ACTION_SENDTO);
			data.setData(Uri.parse("mailto:"
					+ homePageTextView.getText().toString().trim()));
			this.startActivity(data);
			overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
			break;

		default:
			break;
		}
	}

	public class FindBookingAsyncTask extends
			AsyncTask<Object, Integer, BookingConfirmationResponse> {
		private FindBookingBean findBookingBean;

		@Override
		protected BookingConfirmationResponse doInBackground(Object... param) {
			BookingConfirmationResponse response = null;
			findBookingBean = (FindBookingBean) param[0];
			response = appContext
					.getReservationByConfirmationIdAndEmail(findBookingBean);
			return response;
		}

		@Override
		protected void onPostExecute(BookingConfirmationResponse response) {
			super.onPostExecute(response);
			if (response != null) {
				if (response.isHasError()) {
					UIHelper.toastDialogs(SlideFindTeeTimeActivity.this,
							response.getErrorMessage(), R.string.msg_yes);
				} else {
					BookingConfirmationBean bookingConfirmationBean = response
							.getData();
					Intent intent = new Intent(SlideFindTeeTimeActivity.this,
							SlideCancelTeeTimeActivity.class);
					intent.putExtra("bookingConfirmationBean",
							bookingConfirmationBean);
					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					SlideFindTeeTimeActivity.this.startActivity(intent);
					overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
				}
			} else {
				UIHelper.toastDialog(SlideFindTeeTimeActivity.this,
						R.string.cancel_booking_no_found, R.string.msg_yes);
			}
			progressBar.setVisibility(View.GONE);
			emailEditText.setEnabled(true);
			numberEditText.setEnabled(true);
			findBookingButton.setEnabled(true);
			homePageTextView.setEnabled(true);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
			emailEditText.setEnabled(false);
			numberEditText.setEnabled(false);
			findBookingButton.setEnabled(false);
			homePageTextView.setEnabled(false);
		}

	}

	private boolean validation(String email, String confirmation) {
		if (StringUtils.isEmpty(email)) {
			UIHelper.toastDialog(this, R.string.msg_email_field_empty,
					R.string.msg_yes);
			return false;
		}
		if (!StringUtils.isEmail(email)) {
			UIHelper.toastDialog(this, R.string.msg_invalid_email_format,
					R.string.msg_yes);
			return false;
		}
		if (StringUtils.isEmpty(confirmation)) {
			UIHelper.toastDialog(this, R.string.msg_confirmation_field_empty,
					R.string.msg_yes);
			return false;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (findBookingAsyncTask != null
				&& findBookingAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			findBookingAsyncTask.cancel(true);
		}
	}

}
